Ext.define('cceaf.control.storage.SelectProductGrid',{
	extend: 'Ext.grid.GridPanel', 
    alias: 'widget.cceafselectproductgrid',

	/**
	 * 点击触发字段对象
	 */
	triggerField: null,
	
	constructor: function(config){
		config = config || {};

		Ext.define('EntityModel', {
	        extend: 'Ext.data.Model',
	        fields: [
     	        {name: 'productid'}, 
		        {name: 'productno'},
		        {name: 'productname'}, 
		        {name: 'helpcode'},
		        {name: 'specification'}, 
		        {name: 'unit'},
		        {name: 'producefactory'}
    	   ]
	    });

		//定义数据源
	    var ds = new Ext.data.Store({
	    	proxy: {
	    		type: 'ajax',
	    		url: "/" + cceaf.Global.WEB_APP_NAME + '/select/select!query.action?type=PRODUCT',
	    		reader: {
					type: 'json',
					root: 'data',
					totalProperty: 'totalCount',
					messageProperty: 'message'
				}
			},
			
	        listeners: {
	        	'load': function(){
	        		var jr = ds.getProxy().getReader();
	        		var message = jr.jsonData.message;
	        		cceaf.util.ExtHelper.showInfoDialog(message);
	        	},
	        	'loadexception': function(){
	        		var jr = ds.getProxy().getReader();
	        		if(jr.jsonData){
		        		var message = jr.jsonData.message;
		        		cceaf.util.ExtHelper.showErrorDialog(message);
	        		}
	        		else{
	        			cceaf.util.ExtHelper.showErrorDialog("返回无效的JSONDATA数据");
	        		}
	        	}
	        },
	        
	        autoDestroy: true,  
	    	pageSize: szpshang.Global.PAGE_SIZE,
	    	model: 'EntityModel'
	    });
	    
	    var selModel = new Ext.selection.RowModel();
	 
		//查询对象
	    function btnQuery_click(){
	    	var gridPanel = this.up('gridpanel');
	    	
	    	var productno = gridPanel.down("[name=q_product_productno]").getValue();
	    	var productname = gridPanel.down("[name=q_product_productname]").getValue();
	    	var helpcode = gridPanel.down("[name=q_product_helpcode]").getValue();
	    	
	    	ds.proxy.extraParams = {
	    			productno: productno,
	    			productname: productname,
	    			helpcode: helpcode
		    };
	    	ds.load({params:{isquery:true}});
	    }
	    
	  	//重置查询条件
	    function btnReset_click(){
	    	var gridPanel = this.up('gridpanel');
	    	
	    	gridPanel.down("[name=q_product_productno]").setValue("");
	    	gridPanel.down("[name=q_product_productname]").setValue("");
	    	gridPanel.down("[name=q_product_helpcode]").setValue("");
	    }
	    
	 	//定义表格列
	    var columns = [
	    	{xtype: 'rownumberer'},
	        {dataIndex: 'productid',        header: '产品ID',     sortable: true,  hidden: true}, 
	        {dataIndex: 'productno',      header: '产品编号',  width:80,     sortable: true},
	        {dataIndex: 'productname',       header: '产品名称',      sortable: true},
	        {dataIndex: 'helpcode',  header: '助记码',      sortable: true},
	        {dataIndex: 'specification',      header: '规格',      sortable: true},
	        {dataIndex: 'unit',       header: '计量单位',   width:60,   sortable: true},
	        {dataIndex: 'producefactory',  header: '生产厂家',      sortable: true}, 
	        {dataIndex: 'pack',     header: '包装',   width:60,   sortable: true}
	    ];
	    
		config = Ext.apply(config || {}, {
	    	title:'产品信息',
	        border: true,
	        frame: true,
	        store: ds,
	        columns: columns,
	        selModel: selModel,
	        loadMask: {msg:'正在加载数据，请稍侯……'},
	        
	        viewConfig: {
				forceFit:true,
				enableRowBody:true,
				getRowClass : function(record, rowIndex, p, ds){
					return '';
				}
			},
	        
			dockedItems:[{
				xtype : 'toolbar',  
                dock : 'top',  
                items : [
					'产品编号',
					{xtype: 'textfield',name: 'q_product_productno',width: 80},
					'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp',
					'产品名称',{xtype: 'textfield',name: 'q_product_productname',width: 80},
					'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp',
					'助记码',{xtype: 'textfield',name: 'q_product_helpcode',width: 80},
					'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp',
		        	{xtype: "tbfill"},
					{iconCls: 'page_queryIcon', text: ' 查询 ', pressed: true, handler: btnQuery_click},
					'-',
					{iconCls: 'page_resetIcon', text: ' 重置 ', pressed: true, handler: btnReset_click}	
				]
			},{
				xtype: 'pagingtoolbar',
				store: ds,
                dock: 'bottom',  
                displayInfo: true,
                displayMsg:'当前显示 {0} - {1}条记录 /共 {2}条记录',
	            emptyMsg:'无显示数据'
			}],
	        
			listeners: {
	        	'render': function(){
	        		//tbarButton.render(queryGrid.tbar);
	        	},
	        	'itemdblclick': function(grid,rowIndex,e) {
	        		this.selectRow();
	        	},
	        	'keydown': function(e){
	        		if(e.keyCode == 13 && this.getSelectionModel().getLastSelected() != null){
	        			this.selectRow();
	        		}
	        	}
	        },
	        
			selectRow: function(){
				//填充数据到父窗口的控件
				var selectedProductRow = this.getSelectionModel().getLastSelected();
				
				var triggerField = this.triggerField;
				var nameOfProductid = triggerField.nameOfProductid;
				var nameOfProductno = triggerField.nameOfProductno;
				var nameOfProductname = triggerField.nameOfProductname;
				var nameOfHelpcode = triggerField.nameOfHelpcode;
				var nameOfSpecification = triggerField.nameOfSpecification;
				var nameOfUnit = triggerField.nameOfUnit;
				
				var editorGridPane = triggerField.up('cceafdetaileditorgridpanel');
				
				if(editorGridPane != null){
					var selectedRow = editorGridPane.getSelectionModel().getLastSelected();
					
					if(nameOfProductid != null && nameOfProductid != ""){
		    			selectedRow.set(nameOfProductid,selectedProductRow.get("productid"));
					}
					if(nameOfProductno != null && nameOfProductno != ""){
		    			selectedRow.set(nameOfProductno,selectedProductRow.get("productno"));
					}
					if(nameOfProductname != null && nameOfProductname != ""){
		    			selectedRow.set(nameOfProductname,selectedProductRow.get("productname"));
					}
					if(nameOfHelpcode != null && nameOfHelpcode != ""){
		    			selectedRow.set(nameOfHelpcode,selectedProductRow.get("helpcode"));
					}
					if(nameOfSpecification != null && nameOfSpecification != ""){
		    			selectedRow.set(nameOfSpecification,selectedProductRow.get("specification"));
					}
					if(nameOfUnit != null && nameOfUnit != ""){
		    			selectedRow.set(nameOfUnit,selectedProductRow.get("unit"));
					}
				}
				else{
					var triggerFieldInPanel = triggerField.up('panel');
					
	        		if(nameOfProductid != null && nameOfProductid != ""){
	        			triggerFieldInPanel.down('[name=' + nameOfProductid + ']').setValue(selectedProductRow.get("productid"));
	        		}
	        		if(nameOfProductno != null && nameOfProductno != ""){
	        			triggerFieldInPanel.down('[name=' + nameOfProductno + ']').setValue(selectedProductRow.get("productno"));
	        		}
	        		if(nameOfProductname != null && nameOfProductname != ""){
	        			triggerFieldInPanel.down('[name=' + nameOfProductname + ']').setValue(selectedProductRow.get("productname"));
	        		} else {
	        			triggerField.setValue(selectedProductRow.get("productname"));
	        		}
	        		if(nameOfHelpcode != null && nameOfHelpcode != ""){
	        			triggerFieldInPanel.down('[name=' + nameOfHelpcode + ']').setValue(selectedProductRow.get("helpcode"));
	        		}
	        		if(nameOfSpecification != null && nameOfSpecification != ""){
	        			triggerFieldInPanel.down('[name=' + nameOfSpecification + ']').setValue(selectedProductRow.get("specification"));
	        		}
	        		if(nameOfUnit != null && nameOfUnit != ""){
	        			triggerFieldInPanel.down('[name=' + nameOfUnit + ']').setValue(selectedProductRow.get("unit"));
	        		}
				}
        		
        		var selectProductWin = Ext.getCmp('_selectProductWin');
		    	if(selectProductWin){
		    		selectProductWin.hide();
		    	}
			}
	        
		});

		cceaf.control.storage.SelectProductGrid.superclass.constructor.apply(this, arguments);
	}
});